#!/usr/bin/env python3
# #-*-coding: utf-8 -*-
__author__ ="WQY";

import re;
from datetime import datetime

from aa.python_base_02 import *;
import os;
import pymysql;
from util.pymysql import *;
import json;
import hashlib;



def fun_11():
    try:
        for i in list(range(1, 10)):
            print(i);
        a = 1 / 1;
    except BaseException as be:
        print(be);f = open(file="D:/wrnm/text1.txt", mode="w", encoding="utf-8");
    finally:
        print("必须执行的代码块");


def fun_12():
    f = open(file="D:/wrnm/text1.txt", mode="w", encoding="utf-8");
    f.writelines(["cdacdg","\n", "cadscdasdc","\n","你猜猜","\n","cdsacdsacdast"]);
    f.close();

def fun_13():
    f =None;
    try:
        f = open(file="D:/wrnm/text1.txt", mode="r", encoding="utf-8");
        results = f.readlines();
        print(results);
    except BaseException as e:
        print(e);
    finally:
        if f:
            f.close();

def fun_14():
    with open(file="D:/wrnm/text1.txt", mode="r", encoding="utf-8") as f:
        print(f.readlines());

def fun_15():
    with open(file="D:/wrnm/text1.txt", mode="a", encoding="utf-8") as f:
        f.writelines(["追加第一行", "\n", "追加第二行", "\n", "追加第三行"]);

# ======== OS操作 ========
def fun_16():
    print(os.name);
    dest_folder = "D:/temp/aa/bb";
    if not os.path.exists(dest_folder):
        # os.mkdir(dest_folder);
        os.makedirs(dest_folder);
    os.rename("D:/temp/text.txt", "D:/temp/text1.txt");

# ======== mysql操作 ========
def fun_17():
    # sql 语句
    insert_sql = insert_sql = 'insert into user (user_name, password, create_date) ' \
                'values("%s", "%s", "%s")' % ("hj1", "111111", datetime.now().strftime("%Y-%m-%d %H:%M:%S"));
    query_sql = "select * from user";

    connection, cursor = None, None;
    try:
        # 获取链接对象、游标对象
        connection, cursor = pymysql.get_connection_cursor();
        # 执行新增操作
        num = pymysql.execute_();
        print(num);
        # 提交事务
        pymysql.commit_();

        # 执行查询操作
        result = pymysql.query_();
        print(result);
    except BaseException as e:
        print(e);
        # 回滚事务
        pymysql.rollback_();
    finally:
        # 关闭连接和游标
        pymysql.close_();

    def fun_18():
        # json 字符串转 dict
        j = '{"name":"hj", "age":19, "gender":"male"}';
        print(j);
        print(json.loads(j));
        # dict 转 json
        d = {"color": "red", "name": "table", "price": 34.45, "count": 33};
        print(d);
        print(json.dumps(d));
        # 对象转 json 字符串
        user = User(user_name="hahhh", password="wqy020915", email="hj@163.com");
        print(json.dumps(user.__dict__));
        # json 字符串转对象
        j = '{"user_name": "sss", "password": "2222", "email": "aa@163.com"}';
        user.__dict__ = json.loads(j);
        print(user.__dict__);

    # ======== datetime ========
    def fun_19():
        print(datetime.now());
        print(datetime.now().timestamp());
        print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"));
        print(datetime.strptime("1860年5月11日", "%Y年%m月%d日"));

    # ======== hashlib ========
    def fun_20(password):
        password = password + "salthj";
        print(hashlib.md5(password.encode("utf-8")).hexdigest());

if __name__ == '__main__':
    fun_19();


